#define _CRT_SECURE_NO_WARNINGS 1
#include"circulate_SqList.h"

SqList new_node(ElemType x)
{
	SqList s = (SqList)malloc(sizeof(SNode));
	if (s == NULL)
	{
		perror("creat_SqList::");
		return  NULL;
	}
	s->data = x;
	s->next = NULL;
	return s;
}
void insert_gather(SqList pos, ElemType x)
{
	assert(pos);
	SqList s = new_node(x);
	s->next = pos->next;
	pos->next = s;
}
// 创建链表
SqList inint_SqList()
{
	SqList L = (SqList)malloc(sizeof(SNode));
	if (L == NULL)
	{
		perror("creat_SqList::");
		return  NULL;
	}
	L->next = L;
	return L;
}
SqList creat_SqList()
{
	ElemType x;
	SqList L = inint_SqList();
	int choice = 0;
	do
	{
		printf("请输入一个整数:");
		scanf("%d", &x);
		insert_gather(L, x);
		printf("还要继续吗？（输入0退出）\n");
		scanf("%d", &choice);
	} while (choice);
	return L;
}

//输出
void display_SqList(SqList L)
{
	assert(L);
	SqList p = L->next;
	printf("L->");
	while (p!=L)
	{
		printf("%d->", p->data);
		p = p->next;
	}
	printf("L->\n");
}

// 将循环单链表中的元素就地逆置
void reverse_SqList(SqList L)
{
	SqList p = L->next;
	L->next = L;
	while (p!=L)
	{
		SqList q = p->next;
		p->next = L->next;
		L->next = p;
		p = q;
	}
}

//将循环单链表中的元素按照从大到小进行排序
/*子函数，按序插入结点*/
void insert(SqList L, SqList x)
{
	SqList p = L->next;
	SqList q = L;
	while (p != L && p->data > x->data)
	{
		q = p;
		p = p->next;
	}
	x->next = q->next;
	q->next = x;
}
SqList sort_SqList(SqList L)
{
	SqList p = L->next;
	L->next = L;
	while (p != L)
	{
		SqList q = p->next;
		insert(L, p);
		p = q;
	}
	return L;
}